package com.cpit.platform.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.SpringConfigurator;

import com.cpit.platform.log.LogOperator;
import com.fasterxml.jackson.databind.ObjectMapper;

import antlr.collections.List;

/**
 * 信息收发服务.此处实际只记录WebSocket会话。
 */
@Component
@ServerEndpoint(value = "/websock")
public class MessageEndPoint {
	private static ConcurrentHashMap<String, Session> allSessions = new ConcurrentHashMap<String, Session>();
	private static ConcurrentHashMap<String, Session> servletSessionToWsMap = new ConcurrentHashMap<String, Session>();
	private static final String loggerName = MessageEndPoint.class.getName();
	
	public static String KEY_MESSAGE_TYPE_NEW_FAULT = "messageType";
	
	@OnOpen
	public void onOpen(Session session) throws IOException, InterruptedException {
		LogOperator.info(loggerName,"web socket open: " + session.getId());
		String sid = session.getId();
		allSessions.put(sid, session);
	}

	@OnClose
	public void onClose(Session session) throws IOException, InterruptedException {
		LogOperator.info(loggerName,"web socket close: " + session.getId());
		String sid = session.getId();
		allSessions.remove(sid);
		Set<String> ss = servletSessionToWsMap.keySet();
		ArrayList<String> needToDel = new ArrayList<String>();
		for(String sevSe : ss) {
			Session servS = servletSessionToWsMap.get(sevSe);
			if(servS.getId().equals(sid)) {
				needToDel.add(sevSe);
			}
		}
		for(String s : needToDel) {
			servletSessionToWsMap.remove(s);
		}
	}

	@OnMessage
	public void onMessage(String message, Session session)
			throws IOException, InterruptedException {
		LogOperator.info(loggerName,"web socket onMessage: " + message);
		if(!servletSessionToWsMap.containsKey(message)) {
			String sid = session.getId();
			if(!allSessions.containsKey(sid)) {
				allSessions.put(sid, session);
			}
			Session cSess = allSessions.get(sid);
			servletSessionToWsMap.put(message, cSess);
		}
	}

	public static ConcurrentHashMap<String, Session> getAllSessions() {
		return allSessions;
	}
	
	public static void sendMsg(String text) {
		Set<String> ids = allSessions.keySet();
		for (String id : ids) {
			Session peer = allSessions.get(id);
			if (peer != null) {
				try {
					peer.getBasicRemote().sendText(text);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void sendMsgTo(String text, String servSessionId) {
		if(servletSessionToWsMap.containsKey(servSessionId)) {
			Session peer = servletSessionToWsMap.get(servSessionId);
			if (peer != null) {
				try {
					peer.getBasicRemote().sendText(text);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void sendAsynResultMsg(Map<String, Object> msgMap) {
		//发送异步结果消息
		ObjectMapper mapper = new ObjectMapper();
		String ss;
		try {
			ss = mapper.writeValueAsString(msgMap);
			sendMsg(ss);
		} catch (Exception e) {
		}
		
	}
}